<script type="text/javascript">
    $(document).ready(function() {
        initPosterSelect();
        initTypeSelect();
        initFurnishSelect();
        initRegionSelect();
        initCitySelect();
        initCountrySelect();
        initBedroomSelect();
        initBathroomSelect();
        initAmenityCheckboxSet();
        checkAmenities();
    });

    function checkAmenities() {
        var selectedAmenities = new Array();
        $.each($("input[name='amenitycbx']:checked"), function() {
            selectedAmenities.push($(this).val());
        });
        $("#amenityIDs").val(selectedAmenities);
    }

    function initPosterSelect() {
        var posterTypeID = '<c:out value="${model.listing.posterTypeID}"/>';
        $("#listing-posterType").val(posterTypeID);
    }
    function initTypeSelect() {
        var propertyTypeID = '<c:out value="${model.listing.propertyTypeID}"/>';
        $("#listing-propertyType").val(propertyTypeID);
    }    
    function initFurnishSelect() {        
        var furnishTypeID = '<c:out value="${model.listing.furnishTypeID}"/>';
        $("#listing-furnishing").val(furnishTypeID);
    }
    function initStatusSelect() {
        var statusTypeID = '<c:out value="${model.listing.statusTypeID}"/>';
        $("#listing-statusType").val(statusTypeID);
    }
    function initRegionSelect() {
        var region = '<c:out value="${model.listing.region}"/>';
        $("#listing-region").val(region);
    }
    function initCitySelect() {
        var city = '<c:out value="${model.listing.city}"/>';
        $("#listing-city").val(city);
    }
    function initCountrySelect() {
        var country = '<c:out value="${model.listing.country}"/>';
        $("#listing-country").val(country);
    }
    function initBedroomSelect() {
        var numBedrooms = '<c:out value="${model.listing.numBedrooms}"/>';
        $("#listing-numBedrooms").val(numBedrooms);
    }
    function initBathroomSelect() {
        var numBathrooms = '<c:out value="${model.listing.numBathrooms}"/>';
        $("#listing-numBathrooms").val(numBathrooms);
    }
    function initAmenityCheckboxSet() {
        var amenityIDs = '<c:out value="${model.listing.amenityIDs}"/>';
        amenityIDs = amenityIDs.replace("[", "");
        amenityIDs = amenityIDs.replace("]", "");

        var arrAmenityID = amenityIDs.split(',');
        for (var i = 0; i < arrAmenityID.length; i++) {
            var amenityID = arrAmenityID[i].trim();
            $("#amenity-" + amenityID).attr("checked", true);
        }
    }
</script>
